<template>
  <div class="app-container">
    <el-table v-loading="listLoading" :data="collegeList" element-loading-text="Loading" border fit highlight-current-row>
      <el-table-column label="学院ID" width="120" align="center">
        <template slot-scope="scope">
          {{ scope.row.id }}
        </template>
      </el-table-column>
      <el-table-column label="学院名称"  align="center">
        <template slot-scope="scope">
          {{ scope.row.collegeName }}
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center">
        <template slot-scope="scope">
          {{ scope.row.createTime }}
        </template>
      </el-table-column>
      <el-table-column label="修改时间" align="center">
        <template slot-scope="scope">
          {{ scope.row.updateTime }}
        </template>
      </el-table-column>
      <el-table-column label="状态" width="100" align="center">
        <template slot-scope="scope">
          <el-tag type="success" v-if="scope.row.status == 1">正常</el-tag>
          <el-tag type="danger" v-else-if="scope.row.status == 2">已删除</el-tag>
          <el-tag type="danger" v-else>状态异常</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="200">
        <template slot-scope="scope">
          <el-button type="primary" size="mini" @click="handleEdit(scope.row)">编辑</el-button>
          <el-button size="mini" v-if="scope.row.status == 1" type="danger" @click="setStatusAt(scope.row)">删除</el-button>
          <el-button size="mini" v-if="scope.row.status == 2" type="success" @click="setStatusAt(scope.row)">恢复</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="page">
      <el-pagination
        background
        layout="prev, pager, next"
        :total="requestParams.total"
        @current-change="change"
        :page-size="requestParams.pageSize"
      >
      </el-pagination>
    </div>

      <!-- el-dialog -->
    <el-dialog width="35%" title="编辑" :visible.sync="dialogFormVisible">
      <el-form :model="collegeForm">
        <el-form-item label="学院名字" :label-width="formLabelWidth">
          <el-input v-model="collegeForm.collegeName" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" :loading="buttonLoading" @click="toSub">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getCollegeList, updateCollege, getCollegeById } from "@/api/college";
export default {
  data() {
    return {
      collegeList: null,
      listLoading: true,

      collegeForm:{
        collegeName: ""
      },
      requestParams: {
        pageNo: 1,
        pageSize: 10,
        total: 0,
      },
      dialogFormVisible: false,
      buttonLoading: false,
      formLabelWidth: "120px",
    };
  },
  created() {
    this.fetchData();
  },
  methods: {
    fetchData() {
      this.listLoading = true;
      // 假设 getColleges 是获取学院列表的API方法
      getCollegeList(this.requestParams).then(response => {
        this.collegeList = response.data.list;
        this.requestParams.total = response.data.total;
        this.listLoading = false;
      });
    },
    
    change(page) {
      this.requestParams.pageNo = page;
      this.fetchData();
    },

    //编辑学院
    handleEdit(row) {
      this.dialogFormVisible = true;
      console.log("编辑学院:", row);
      this.collegeForm.collegeName = row.collegeName;
      this.collegeForm.id = row.id;
    },

    //处理响应
    opt(res){
      if(res.code == 200){
          this.$message({
            type: 'success',
            message: '操作成功!'
          });
        }else {
          this.$message({
            type: 'error',
            message: res.msg
          });  
        }
    },
    // 编辑的提交
    toSub(row) {
      
      this.buttonLoading = true;

      updateCollege(this.collegeForm).then((res) => {
        this.opt(res)
        this.fetchData()
      });
      this.buttonLoading = false;
      this.dialogFormVisible = false;
    },
    //修改状态
    setStatus(row){
      // this.setStatusAt()
      updateCollege({id:row.id,status:row.status}).then(res => {
        this.opt(res)
        this.fetchData()
      })
      // fetchData()
    },

    setStatusAt(row) {
        this.$confirm('修改该学院状态, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.setStatus(row)
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });          
        });
      }
  }
};
</script>

<style scoped>
.page {
  margin-top: 15px;
  text-align: center;
}

.app-container{
  padding: 20px;
}
</style>